package com.tudulist.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.tudulist.dao.TodoDao;
import com.tudulist.model.Todo;

@Repository
public class TodoDaoImpl implements TodoDao {

	private static final long serialVersionUID = 1L;

	@PersistenceContext
	EntityManager em;

	@SuppressWarnings("unchecked")
	public List<Todo> findAll() {
		return em.createQuery("from Todo").getResultList();
	}

	public Todo find(long id) {
		return (Todo) em.find(Todo.class, id);
	}

	public Todo save(Todo todo) {
		return em.merge(todo);
	}

	public void remove(long id) {
		Todo todo = (Todo) em.find(Todo.class, id);
		if (null != todo) {
			em.remove(todo);
		}

	}

	@SuppressWarnings("unchecked")
	public List<Todo> findAll(Long todolistId) {
		return em.createQuery("from Todo where todolists_id=" + todolistId.toString()).getResultList();
	}

}
